#!/bin/bash
echo "\033[31m ================================================= \033[0m"
echo "\033[31m 开始打包... \033[0m"
echo "\033[31m ================================================= \033[0m"

Project_Dir="./.."
Project_Name="BabaLink"
Scheme="BabaLink"
SDK="iphoneos"
Configuration="Debug"
BuildDir="./build"
ArchivePath="${BuildDir}/${Scheme}.xcarchive"
ExportOptionsPlist="./ExportOptions.plist"
ProvisioningProfilePath="./profile/88IRBetaAdHoc.mobileprovision"
CodeSignIdentify="iPhone Distribution: 88 Meeting Limited (93F6B9Q7ZC)"
ExportPath="${BuildDir}"
MobileprovisionPlist="./profile/BetaAdHoc.plist"
ProjectPlistPath="${Project_Dir}/BabaLink/Info.plist"
BundleID="com.88meeting.ir.beta"
AppIcon="AppIconBeta"
log=1
filePath="${Project_Dir}/${Project_Name}.xcodeproj/project.pbxproj"

KeyPwd="123456"
Key="ios_distribution.p12"
KEY_CHAIN=ios-build.keychain
KEY_CHAIN_PWD=travis

function installDependencies() {
    echo "\033[31m ========安装依赖 \033[0m"
#    gem update --system
#    gem sources --remove https://rubygems.org/
#    gem sources -a https://gems.ruby-china.com/
#    gem sources -l
    echo -e "\033[34m =========安装 sentry-cli \033[0m"
    brew install getsentry/tools/sentry-cli
    echo -e "\033[34m =========安装 fir-cli \033[0m"
# 1.6.11版本有问题
    gem install fir-cli -v 1.6.10
    echo -e "\033[34m =========安装 cocopods \033[0m"
    gem install cocoapods
}

function updatePod() {
    echo "\033[31m ========更新POD \033[0m"
    basepath=$(cd 'dirname $0'; pwd)
    cd ${Project_Dir}
    pod repo update
    pod update --no-repo-update
    cd ${basepath}
}

function importCer() {
    echo "\033[31m =========导入证书 \033[0m"
    security create-keychain -p $KEY_CHAIN_PWD $KEY_CHAIN
    security default-keychain -s $KEY_CHAIN
    security unlock-keychain -p $KEY_CHAIN_PWD $KEY_CHAIN
    security set-keychain-settings -t 3600 -u $KEY_CHAIN
    security import ./certs/$Key -k ~/Library/Keychains/$KEY_CHAIN -P $KeyPwd -T /usr/bin/codesign
    security set-key-partition-list -S apple-tool:,apple: -s -k $KEY_CHAIN_PWD $KEY_CHAIN
}

function deleteCer() {
    security delete-keychain ios-build.keychain
    security default-keychain -s login.keychain
    rm -f ~/Library/MobileDevice/Provisioning\ Profiles/
}

function importProvision() {
    echo "\033[31m ========导入mobileprovision \033[0m"
    security cms -D -i ${ProvisioningProfilePath} > $MobileprovisionPlist
    mobileprovisionUUID=$(/usr/libexec/PlistBuddy -c "Print UUID" "${MobileprovisionPlist}")
    echo "\033[34m ========mobile provision UUID:${mobileprovisionUUID} \033[0m"
    mkdir -p ~/Library/MobileDevice/Provisioning\ Profiles
    CPCMD="cp"
    CPCMD=$CPCMD" ${ProvisioningProfilePath} ~/Library/MobileDevice/Provisioning\ Profiles/${mobileprovisionUUID}.mobileprovision"
    echo "\033[34m ========拷贝mobileprovision:${CPCMD} \033[0m"
    eval $CPCMD
}

function manualSign() {
    echo "\033[31m ========修改为手动签名 \033[0m"
    sed -i "" "s/^.*CODE_SIGN_STYLE.*$/                CODE_SIGN_STYLE = Manual;/" $filePath
}

function updateAppIcon() {
    echo "\033[31m ========修改AppIcon为${AppIcon} \033[0m"
    sed -i "" "s/^.*ASSETCATALOG_COMPILER_APPICON_NAME.*$/                ASSETCATALOG_COMPILER_APPICON_NAME = ${AppIcon};/" $filePath
}

function updateBundleID() {
    echo "\033[31m ========修改Bundle ID为Beta版 \033[0m"
    sed -i "" "s/^.*PRODUCT_BUNDLE_IDENTIFIER.*$/                PRODUCT_BUNDLE_IDENTIFIER = ${BundleID};/" $filePath
    oldBundleID=$(/usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" "${ProjectPlistPath}")
    /usr/libexec/PListBuddy -c "Set :CFBundleIdentifier ${BundleID}" "${ProjectPlistPath}"
    newBundleID=$(/usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" "${ProjectPlistPath}")
    echo "\033[34m ========${oldBundleID} 改为 ${newBundleID} \033[0m"
}

function updateBuildNumber() {
    echo "\033[31m ========修改build number \033[0m"
    buildNumber=$(TZ=Asia/Shanghai date +"%Y-%m-%d %H:%M")
    /usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${buildNumber}" "${ProjectPlistPath}"
    echo "\033[34m ========当前打包版本号：${buildNumber} \033[0m"
}

function clearCache() {
    echo "\033[31m ========清理缓存 \033[0m"
    rm -rf ~/Library/Developer/Xcode/DerivedData
    rm -rf ./build
}

function archive() {
    echo "\033[31m ========开始归档 \033[0m"
    ArchiveCMD="xcodebuild archive"
    ArchiveCMD=${ArchiveCMD}" -workspace ${Project_Dir}/${Project_Name}.xcworkspace"
    ArchiveCMD=${ArchiveCMD}" -scheme ${Scheme}"
    ArchiveCMD=${ArchiveCMD}" -configuration ${Configuration}"
    ArchiveCMD=${ArchiveCMD}" -sdk ${SDK}"
    ArchiveCMD=${ArchiveCMD}" -archivePath ${ArchivePath}"
#    ArchiveCMD=${ArchiveCMD}" CODE_SIGN_IDENTITY=\"${CodeSignIdentify}\""
#    ArchiveCMD=${ArchiveCMD}" PROVISIONING_PROFILE=\"${mobileprovisionUUID}\""
    if [ ${log} -eq 0 ] ; then
    ArchiveCMD=${ArchiveCMD}" > /dev/null"
    fi

    echo "\033[34m ========归档:${ArchiveCMD} \033[0m"
    eval $ArchiveCMD
}

function exportArchive() {
    echo "\033[31m ========开始导出归档 \033[0m"
    ExportArchiveCMD="xcodebuild -exportArchive"
    ExportArchiveCMD=${ExportArchiveCMD}" -archivePath ${ArchivePath}"
    ExportArchiveCMD=${ExportArchiveCMD}" -exportOptionsPlist ${ExportOptionsPlist}"
    ExportArchiveCMD=${ExportArchiveCMD}" -exportPath ${ExportPath}"
#    ExportArchiveCMD=${ExportArchiveCMD}" CODE_SIGN_IDENTITY=\"${CodeSignIdentify}\""
#    ExportArchiveCMD=${ExportArchiveCMD}" PROVISIONING_PROFILE=\"${mobileprovisionUUID}\""
    if [ ${log} -eq 0 ] ; then
    ExportArchiveCMD=${ExportArchiveCMD}" > /dev/null"
    fi
    echo "\033[34m ========导出归档:${ExportArchiveCMD} \033[0m"
    eval ${ExportArchiveCMD}
}

function exportDsym() {
    if [[ ${Configuration} == "Release" ]]; then
        echo "\033[31m ========导出dsym文件 \033[0m"
        cd ${BuildDir}
        cp -R ./${Scheme}.xcarchive/dSYMs/${Scheme}.app.dSYM ./
        zip -r -9 "./${Scheme}.app.dSYM.zip" "./${Scheme}.app.dSYM"
    fi
}

function uploadFir() {
    echo "\033[31m ================================================= \033[0m"
    echo "\033[31m 开始上传FIR... \033[0m"
    echo "\033[31m ================================================= \033[0m"
    #gem list rest-client
    log=$(git log -1 | tail -n 1)
    echo "\033[34m 上次提交日志：${log} \033[0m"
    cmd="fir publish ${BuildDir}/${Scheme}.ipa -T 0d416f076e5567f7ea835995ebfbc7e5 -c \"${log}\""
    echo "\033[34m ${cmd} \033[0m"
    eval ${cmd}

}

# 删除证书
#deleteCer

# 导入证书
#importCer

# 安装依赖
#installDependencies

# 更新POD
#updatePod

#########################

# 获取mobileprovision文件
#importProvision

# 设置为手动签名
#manualSign

#修改AppIcon
updateAppIcon

#修改Bundle ID
updateBundleID

#修改buildNumber
updateBuildNumber

# 清理缓存
#clearCache

##归档
archive

# 导出归档
exportArchive

# 导出dsym
exportDsym

# 导出上传fir
uploadFir


echo "\033[31m 打包上传完成... \033[0m"

